{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from copyheaders import headers_raw_to_dict\n",
    "import re\n",
    "import ast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{b'Accept': b'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',\n",
       " b'Accept-Encoding': b'gzip, deflate, br',\n",
       " b'Accept-Language': b'zh-CN,zh;q=0.9',\n",
       " b'Cache-Control': b'max-age=0',\n",
       " b'Connection': b'keep-alive',\n",
       " b'Cookie': b'QiHooGUID=207FA117BBF15E1E2AF193C68327886B.1603074826606; __guid=15484592.1509751643168084500.1603074828327.017; __md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; so_md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; dpr=1; isafe=1; webp=1; so_huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; __huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; _uc_m2=null; _uc_mid=4dccb2a30402372bf688f462811d31aa; __mid=4dccb2a30402372bf688f462811d31aa; soid=SJVkSS3znotf_0R(Xdnv1(cVy8Q2SX9wN-4S!v7yK5; __DC_gid=9114931.167822696.1604019949915.1609743830170.92; __gid=9114931.167822696.1604019949915.1614914821941.126; _uc_silent=1; _S=lhcmke4k37oumaqithvbvajcg7; gtHuid=1; count=22; erules=p1-4%7Cecl-1%7Cp2-2%7Cp3-3%7Ckd-1',\n",
       " b'Host': b'www.so.com',\n",
       " b'Sec-Fetch-Mode': b'navigate',\n",
       " b'Sec-Fetch-Site': b'same-origin',\n",
       " b'Sec-Fetch-User': b'?1',\n",
       " b'Upgrade-Insecure-Requests': b'1',\n",
       " b'User-Agent': b'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360SE/12.2.1862.0'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一：使用 copyheaders 库转换 headers\n",
    "headers_str = '''\n",
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\n",
    "Accept-Encoding: gzip, deflate, br\n",
    "Accept-Language: zh-CN,zh;q=0.9\n",
    "Cache-Control: max-age=0\n",
    "Connection: keep-alive\n",
    "Cookie: QiHooGUID=207FA117BBF15E1E2AF193C68327886B.1603074826606; __guid=15484592.1509751643168084500.1603074828327.017; __md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; so_md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; dpr=1; isafe=1; webp=1; so_huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; __huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; _uc_m2=null; _uc_mid=4dccb2a30402372bf688f462811d31aa; __mid=4dccb2a30402372bf688f462811d31aa; soid=SJVkSS3znotf_0R(Xdnv1(cVy8Q2SX9wN-4S!v7yK5; __DC_gid=9114931.167822696.1604019949915.1609743830170.92; __gid=9114931.167822696.1604019949915.1614914821941.126; _uc_silent=1; _S=lhcmke4k37oumaqithvbvajcg7; gtHuid=1; count=22; erules=p1-4%7Cecl-1%7Cp2-2%7Cp3-3%7Ckd-1\n",
    "Host: www.so.com\n",
    "Sec-Fetch-Mode: navigate\n",
    "Sec-Fetch-Site: same-origin\n",
    "Sec-Fetch-User: ?1\n",
    "Upgrade-Insecure-Requests: 1\n",
    "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360SE/12.2.1862.0\n",
    "'''\n",
    "headers_bytes = headers_str.encode('utf-8') # 把字符串转换成二进制格式\n",
    "headers = headers_raw_to_dict(headers_bytes)\n",
    "headers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',\n",
       " 'Accept-Encoding': 'gzip, deflate, br',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Cache-Control': 'max-age=0',\n",
       " 'Connection': 'keep-alive',\n",
       " 'Cookie': 'QiHooGUID=207FA117BBF15E1E2AF193C68327886B.1603074826606; __guid=15484592.1509751643168084500.1603074828327.017; __md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; so_md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; dpr=1; isafe=1; webp=1; so_huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; __huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; _uc_m2=null; _uc_mid=4dccb2a30402372bf688f462811d31aa; __mid=4dccb2a30402372bf688f462811d31aa; soid=SJVkSS3znotf_0R(Xdnv1(cVy8Q2SX9wN-4S!v7yK5; __DC_gid=9114931.167822696.1604019949915.1609743830170.92; __gid=9114931.167822696.1604019949915.1614914821941.126; _uc_silent=1; _S=lhcmke4k37oumaqithvbvajcg7; gtHuid=1; count=22; erules=p1-4%7Cecl-1%7Cp2-2%7Cp3-3%7Ckd-1',\n",
       " 'Host': 'www.so.com',\n",
       " 'Sec-Fetch-Mode': 'navigate',\n",
       " 'Sec-Fetch-Site': 'same-origin',\n",
       " 'Sec-Fetch-User': '?1',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360SE/12.2.1862.0'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二：正则替换\n",
    "headers_re = re.sub('(.*?): (.*?)\\n',r\"'\\1':'\\2',\\n\",headers_str)\n",
    "headers_re = '{' + headers_re.replace('\\n','') +'}'\n",
    "headers_re = ast.literal_eval(headers_re) # 字符串转字典\n",
    "headers_re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',\n",
       " 'Accept-Encoding': 'gzip, deflate, br',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Cache-Control': 'max-age=0',\n",
       " 'Connection': 'keep-alive',\n",
       " 'Cookie': 'QiHooGUID=207FA117BBF15E1E2AF193C68327886B.1603074826606; __guid=15484592.1509751643168084500.1603074828327.017; __md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; so_md=3o4dccb2a304016030748284382372bf688f462811d31aa.11; dpr=1; isafe=1; webp=1; so_huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; __huid=11NRzI7WT44YhBaM%2BchNkDmYNI1nT7eb7SCmfQDCLdkOM%3D; _uc_m2=null; _uc_mid=4dccb2a30402372bf688f462811d31aa; __mid=4dccb2a30402372bf688f462811d31aa; soid=SJVkSS3znotf_0R(Xdnv1(cVy8Q2SX9wN-4S!v7yK5; __DC_gid=9114931.167822696.1604019949915.1609743830170.92; __gid=9114931.167822696.1604019949915.1614914821941.126; _uc_silent=1; _S=lhcmke4k37oumaqithvbvajcg7; gtHuid=1; count=22; erules=p1-4%7Cecl-1%7Cp2-2%7Cp3-3%7Ckd-1',\n",
       " 'Host': 'www.so.com',\n",
       " 'Sec-Fetch-Mode': 'navigate',\n",
       " 'Sec-Fetch-Site': 'same-origin',\n",
       " 'Sec-Fetch-User': '?1',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360SE/12.2.1862.0'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法三：自定义函数\n",
    "def change(str):\n",
    "    result = {}  # 初始化返回结果\n",
    "    str_new = str.replace(': ', ':').replace('    ', '')  # 第一步，将里面的冒号空格转换为冒号,然后消掉tab\n",
    "    str_list = str_new.split('\\n')  # 第二部，将字符串按行分割，可能会出现列表的第一个元素和最后一个元素为空字符串的情况\n",
    "    for i in str_list:\n",
    "        if i:  # 做个筛选，过滤掉空字符串\n",
    "            # 此时的i是字符串，现在要将它转为键值对\n",
    "            temp = i.split(':')\n",
    "            key = temp[0]\n",
    "            value =':'.join(temp[1:])#前面分割的时候会出现很多个：所以这里要还原\n",
    "            result[key] = value  # 将键值对赋值给字典\n",
    "    return result\n",
    "\n",
    "# for i, j in change(headers_str).items():\n",
    "#     print(i + ':' + j)\n",
    "headers_result = change(headers_str)\n",
    "headers_result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方法四：postman\n",
    "\n",
    "![](https://img2018.cnblogs.com/blog/1459189/201908/1459189-20190827120602168-63476971.png)\n",
    "\n",
    "![](https://img2018.cnblogs.com/blog/1459189/201908/1459189-20190827120716500-763565317.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 方法五：使用 curlconverter\n",
    "### 网站地址：https://curl.trillworks.com\n",
    "\n",
    "![](https://curl.trillworks.com/images/newscreenshot.png)\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/202009111720260.png)\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/20200911173041768.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
